/**
 * zmn 花名册模块-离职信息表
 * @author tanghongjun
 * @since 2020-05-14 10:56:45
 */
let baseUrl;
layui.define(['index', 'form', 'table', 'laypage', 'zmnApp', 'layedit', 'laydate', 'upload'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    var upload = layui.upload;
    let zmnApp = layui.zmnApp;
    baseUrl = zmnApp.SYS_BASE_URL;

    if(json.staffLeaveOffice.lastWorkTime != null){
        laydate.render({elem: "#lastWorkTime", type: "date", value: new Date(json.staffLeaveOffice.lastWorkTime)});
    }else{
        laydate.render({elem: "#lastWorkTime", type: "date"});
    }
    form.render(null, "zmn-form");

    upload.render({
        elem: '#upload'
        ,url: zmnApp.SYS_BASE_URL +'/common/uploadFile.action' //改成您自己的上传接口
        ,data: {folderName: 'staffLeaveOffice'}
        ,accept: 'file' //普通文件
        ,multiple: false
        ,size:1024*25
        ,exts: 'xlsx|xls|csv|pdf|ppt|pptx|doc|docx|rar|tar|zip|7z|bmp|jpg|jpeg|png|gif|webp'
        ,done: function(res){
            var uploadFiles = document.getElementsByClassName('uploadFile');
            var size = uploadFiles.length;

            if (size >= 3){
                layer.alert('添加失败，附件最多不超过三个！');
                return false;
            }

            let fileSrc = $('#fileSrc').attr('href');
            let fileSrc2 = $('#fileSrc2').attr('href');
            let fileSrc3 = $('#fileSrc3').attr('href');

            var addName = "";
            if (size == 0) {
                addName = "";
            } else {
                if (fileSrc == undefined) {
                    addName = "";
                } else if (fileSrc2 == undefined) {
                    addName = 2;
                } else if (fileSrc3 == undefined) {
                    addName = 3;
                }
            }

            var fileSize = 0;
            fileSize = res.data.size * 1.0 / 1024;
            fileSize = fileSize.toFixed(2);
            fileSize = fileSize + 'KB';

            $("#files").append('<div id="file">\n' +
                '                            <div class="layui-input-block">\n' +
                '                                <div class="layui-input-inline" style="width: auto">\n' +
                '                                    <span id="fileSrc'+ addName +'" href="' + res.data.src + '" key="' + res.data.name + '" class="layui-form-label uploadFile" style="width: auto;">' + res.data.name + '</span>\n' +
                '                                    <span id="fileSize'+ addName +'" value="'+ res.data.size +'" class="layui-form-label" style="width: auto; color: #9F9F9F;">' + fileSize + '</span>\n' +
                '                                    <a class="layui-form-label" onclick="deleteFile(\'fileSrc'+ addName +'\')" style="width:auto;color: #00a0e9;cursor:pointer">\n' +
                '                                        删除\n' +
                '                                    </a>\n' +
                '<a class="layui-form-label" onclick="downLoadFile(\''+ res.data.src +'\')" style="width:auto;color: #00a0e9;cursor:pointer">\n' +
                '                                    下载\n' +
                '                                </a>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="layui-split">&nbsp;</div>\n' +
                '                        </div>');

            form.render();
            layer.msg('上传成功');
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;

        data['leaveFileSrc'] = $('#fileSrc').attr('href');
        data['leaveFileSize'] = $('#fileSize').attr('value');
        data['leaveFileName'] = $('#fileSrc').attr('key');

        data['leaveFileSrc2'] = $('#fileSrc2').attr('href');
        data['leaveFileSize2'] = $('#fileSize2').attr('value');
        data['leaveFileName2'] = $('#fileSrc2').attr('key');

        data['leaveFileSrc3'] = $('#fileSrc3').attr('href');
        data['leaveFileSize3'] = $('#fileSize3').attr('value');
        data['leaveFileName3'] = $('#fileSrc3').attr('key');

        // if ((data['leaveFileSrc'] == null || data['leaveFileSrc'] == undefined)
        //     && (data['leaveFileSrc2'] == null || data['leaveFileSrc2'] == undefined)
        //     && (data['leaveFileSrc3'] == null || data['leaveFileSrc3'] == undefined)) {
        //     layer.msg("离职扫描件不能为空！");
        //     return false;
        // }
        if(zmnUtil.isEmptyNull(data['status'])){
                layer.msg("离职状态不能为空！");
                return false;
        }
        if(zmnUtil.isEmptyNull(data['leaveReason'])){
            layer.msg("离职原因不能为空！");
            return false;
        }
        if(zmnUtil.isEmptyNull(data['leaveSuccessor'])){
            layer.msg("离职交接人不能为空！");
            return false;
        }
        if(zmnUtil.isEmptyNull(data['lastWorkTime'])){
            layer.msg("最后工作时间不能为空！");
            return false;
        }

        var url = zmnApp.SYS_BASE_URL +"/staff/staffleaveoffice/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                if(result.status == 200) {
                    parent.reload(result.message);
                    cancel();
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        })
        return false;
    });
    //对外暴露的接口
    exports('plat_staffleaveoffice_detail', {});
});

function deleteFile(element) {
    var compoent = document.getElementById(element);
    var block = compoent.parentNode.parentNode;
    var file = compoent.parentNode.parentNode.parentNode;
    var files = compoent.parentNode.parentNode.parentNode.parentNode;
    files.removeChild(file);
    form.render();
}

function downLoadFile(fileSrc) {
    let downUrl = baseUrl+"/common/download.action?urlFile="+fileSrc;
    // let downUrl = "/contract/record/downLoad.action?file="+fileSrc;
    window.open(downUrl, "_parent");
}

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}